package com.tanguy.user.ui.me.activity.account.join

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import cn.sinata.xldutils_kotlin.activity.SelectPhotoDialog
import com.tanguy.user.Const
import com.tanguy.user.R
import com.tanguy.user.base.ImageViewTang
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.bean.HotelRegister
import com.tanguy.user.bean.HotelTypeBean
import com.tanguy.user.net.http.HttpManager
import com.tanguy.user.ui.map.activity.MapFindCoordinateActivity
import com.tanguy.user.ui.window.MessageWindow
import com.tanguy.user.ui.window.OnePickerViewPopupWindow
import com.tanguy.user.utlis.extend.*
import org.jetbrains.anko.find
import org.jetbrains.anko.startActivityForResult

/**
 * Created by dab on 2017/10/10 0010 15:05
 * 酒店加盟
 */
class HotelJoinActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_hotel_join
    val mHotelRegister by lazy {
        HotelRegister()
    }
    var mHotelType: HotelTypeBean? = null
    override fun initView() {
        super.initView()
        title = "酒店加盟"
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        HttpManager.getHotelType()
                .requestSucceed(this) {
                    mHotelType = it
                    loge(it)
                }
    }

    override fun initEvent() {
        super.initEvent()
        //类型
        click(R.id.ttv_type) {
            if (mHotelType == null) {
                showToast("获取类型失败")
                return@click
            }
            showPopupWindow(OnePickerViewPopupWindow(this, mHotelType!!.typeList) { pos, msg ->
                setTextRight(R.id.ttv_type,msg!!,R.color.color_333333)
                mHotelRegister.typeid = mHotelType!!.type[pos].id.toString()
                mHotelRegister.typeString = msg
            })
        }
        //特色
        click(R.id.ttv_tese) {
            if (mHotelType == null) {
                showToast("获取特色失败")
                return@click
            }
            showPopupWindow(OnePickerViewPopupWindow(this, mHotelType!!.teseList) { pos, msg ->
                setTextRight(R.id.ttv_tese,msg!!,R.color.color_333333)
                mHotelRegister.featureid = mHotelType!!.tese[pos].id.toString()
                mHotelRegister.featureString = msg
            })
        }
        //选择地区
        click(R.id.ttv_region) {
            startActivityForResult<RegionActivity>(Const.RequestCode.REGION_TYPE)
        }
        //选择详细地址
        click(R.id.ttv_detailed_address) {
            if (TextUtils.isEmpty(mHotelRegister.cityCode)) {
                showToast("请先选择城市")
                return@click
            }
            rxPermissionsLocation {
                startActivityForResult<MapFindCoordinateActivity>(Const.RequestCode.HOTEL_ADDRESS
                        , "cityCode" to mHotelRegister.cityCode
                        , "cityName" to mHotelRegister.cityCodeString
                        , "type" to 1
                )
            }

        }
        //酒店展示图
        click(R.id.iv_showimg) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMG_SHOW)
        }
        //营业执照
        click(R.id.iv_businImg) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMG_BUSIN)
        }
        //特种行业许可证
        click(R.id.iv_special) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMG_SPECIAL)
        }
        //卫生许可证
        click(R.id.iv_healthImg) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMG_HEALTH)
        }
        //消防安全许可证
        click(R.id.iv_fireImg) {
            startActivityForResult<SelectPhotoDialog>(Const.RequestCode.IMG_FIRE)
        }
        //提交
        click(R.id.btn_submit) {
            setEditText()
            if (!checkHotelRegister(mHotelRegister)) return@click
            registerHotel(mHotelRegister)
        }
    }

    private fun registerHotel(hotelRegister: HotelRegister) {
        loge(hotelRegister.toString())
        //注册开始
        HttpManager.uploadFile(this, hotelRegister.showimg)
                .flatMap {
                    loge(it)
                    hotelRegister.showimgUrl = it
                    HttpManager.uploadFile(this, hotelRegister.businImg)
                }
                .flatMap {
                    loge(it)
                    hotelRegister.businImgUrl = it
                    HttpManager.uploadFile(this, hotelRegister.specialImg)
                }
                .flatMap {
                    loge(it)
                    hotelRegister.specialImgUrl = it
                    HttpManager.uploadFile(this, hotelRegister.healthImg)
                }
                .flatMap {
                    loge(it)
                    hotelRegister.healthImgUrl = it
                    HttpManager.uploadFile(this, hotelRegister.fireImg)
                }
                .flatMap {
                    loge(it)
                    hotelRegister.fireImgUrl = it
                    HttpManager.hotelLeague(hotelRegister)
                }
                .requestSucceed(this) {
                    showPopupWindow(MessageWindow(this, "您已成功提交审核，我们将尽快与你联系，请保持电话通畅！", false) {
                        finish()
                    })
                }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == Const.RequestCode.REGION_TYPE && data != null) {
                val codeId = data.getStringExtra(Const.IntentCode.CITY_CODE)
                val cityProvinceString = data.getStringExtra(Const.IntentCode.CITY_PROVINCE_STRING)
                val codeString = data.getStringExtra(Const.IntentCode.CITY_STRING)
                setTextRight(R.id.ttv_region,cityProvinceString,R.color.color_333333)
                mHotelRegister.cityCode = codeId
                mHotelRegister.cityProvinceString = cityProvinceString
                mHotelRegister.cityCodeString = codeString
            }
            if (requestCode == Const.RequestCode.IMG_SHOW && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_showimg).setImageByFile(path)
                mHotelRegister.showimg = path
            }
            if (requestCode == Const.RequestCode.IMG_BUSIN && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_businImg).setImageByFile(path)
                mHotelRegister.businImg = path
            }
            if (requestCode == Const.RequestCode.IMG_SPECIAL && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_special).setImageByFile(path)
                mHotelRegister.specialImg = path
            }
            if (requestCode == Const.RequestCode.IMG_HEALTH && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_healthImg).setImageByFile(path)
                mHotelRegister.healthImg = path
            }
            if (requestCode == Const.RequestCode.IMG_FIRE && data != null) {
                val path = data.getStringExtra(SelectPhotoDialog.PATH)
                find<ImageViewTang>(R.id.iv_fireImg).setImageByFile(path)
                mHotelRegister.fireImg = path
            }
            if (requestCode == Const.RequestCode.HOTEL_ADDRESS && data != null) {
                val address = data.getStringExtra("address")
                val latitude = data.getStringExtra("latitude")
                val longitude = data.getStringExtra("longitude")
                setTextRight(R.id.ttv_detailed_address,address,R.color.color_333333)
                mHotelRegister.hotelAddr = address
                mHotelRegister.lat = latitude
                mHotelRegister.lon = longitude
            }
        }
    }

    private fun setEditText() {
        mHotelRegister.name = getEditTextString(R.id.et_name)
        mHotelRegister.people = getEditTextString(R.id.et_name_person)
        mHotelRegister.phone = getEditTextString(R.id.et_phone_person)
    }


}